
var myAtoi = function (s) {
    let length = s.length, rightOpr = '', ptr = 0, current = '+', res = 0;
    let reg = /[a-zA-Z]/, continueopt = 0, space = 0;
    while(ptr < length) {
        if(s.charAt(ptr) === ' ') space++
        else break;
        ptr++
    }
    ptr = space
    while (ptr < length) {
        if(continueopt > 1)break
        if (s.charAt(ptr) >= '0' && s.charAt(ptr) <= '9' || s.charAt(ptr) === '.') {
            if (s.charAt(ptr) !== '0' || rightOpr !== '') {
                rightOpr += s.charAt(ptr)
            }
        } else if (s.charAt(ptr) === '+' || s.charAt(ptr) === '-') {
            continueopt++
            if (current === '+') {
                res += Number(rightOpr)
            } else {
                res -= Number(rightOpr)
            }
            current = s.charAt(ptr)
            rightOpr = ''
        } else if (reg.test(s.charAt(ptr))) {
            break;
        } else if(s.charAt(ptr) === ' ') {
            break;
        }
        ptr++
    }
    if (rightOpr !== '') {
        if (current === '+') {
            res += Number(rightOpr)
        } else {
            res -= Number(rightOpr)
        }
    }
    if (res > 2 ** 31 - 1) return 2 ** 31 - 1
    if (res < -(2 ** 31)) return -(2 ** 31)
    return Math.floor(res)
};
console.log(myAtoi('   -42'));
console.log(myAtoi('-0042'));
console.log(myAtoi('"3.14159"'));
console.log(myAtoi('"+ -12"'));
console.log(myAtoi("   +0 123"));
// console.log(Number(''));